﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

namespace TMS
{
    public partial class EditUsersScreen : Form
    {
        public Users ActiveUser;
        public MySqlDataAdapter MyDA = new MySqlDataAdapter();

        public EditUsersScreen()
        {
            InitializeComponent();
        }

        public EditUsersScreen(Users ActiveUserLogedon)
        {
            InitializeComponent();
            this.ActiveUser = ActiveUserLogedon;
        }

        private void EditUsersScreen_Load(object sender, EventArgs e)
        {
            //string connString = @"server=localhost;userid=root;password=pass;database=mydb";
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection mysqlCon = new MySqlConnection(connString);
            mysqlCon.Open();

            
            string sqlSelectAll = "SELECT * from Users";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, mysqlCon);

            DataTable table = new DataTable();
            MyDA.Fill(table);

            BindingSource bSource = new BindingSource();
            bSource.DataSource = table;

            UsersDataGridView.DataSource = bSource;
            mysqlCon.Close();
        }

        private void AddUserButton_Click(object sender, EventArgs e)
        {
            NewUser frm = new NewUser();
            frm.ShowDialog();
        }

        private void DeleteUserButton_Click(object sender, EventArgs e)
        {
            string todelete = UsersDataGridView.SelectedRows[0].Cells[3].Value.ToString();
            string sqlDelete = "DELETE FROM Users WHERE Username='"+todelete+"'";

            //string cs = @"server=localhost;userid=root;password=pass;database=mydb";
            string connString = ConfigurationManager.ConnectionStrings["MyDBConnectionString"].ConnectionString;

            MySqlConnection conn = null;
            conn = new MySqlConnection(connString);
            conn.Open();
            MySqlCommand cmd = new MySqlCommand(sqlDelete, conn);
            cmd.ExecuteNonQuery();
            conn.Close();

            //UsersDataGridView.Update();

            EditUsersScreen_Load(sender, e);
        }

       




    }
}
